﻿@using CommandLib
@model BackEnd.Models.ReportAccountModel
@{
    ViewBag.Title = "Account";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>List account</h2>    
<table width="50%">
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.FirstName)
            @Html.HiddenFor(m=>m.AccountId)
            @Html.HiddenFor(m=>m.ListId)
        </td>
        <td>@Html.TextBoxFor(m=>m.FirstName)</td>
        <td class="editor-label">@Html.LabelFor(m=>m.LastName)</td>
        <td>@Html.TextBoxFor(m=>m.LastName)</td>
    </tr>
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.Email)</td>
        <td>@Html.TextBoxFor(m=>m.Email)</td>
        <td class="editor-label">@Html.LabelFor(m=>m.CountryId)</td>
        <td>@Html.DropDownListFor(m=>m.CountryId, new SelectList(Model.ListCountry, "ID", "CountryName"), "----All----")</td>
    </tr>
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.PartnerId)</td>
        <td>@Html.DropDownListFor(m=>m.PartnerId, new SelectList(Model.ListPartner, "Id", "PartnerCode"), "----All----")</td>
        <td class="editor-label">@Html.LabelFor(m=>m.Status)</td>
        <td>
            <select id="Status" name="Status">
                <option>----All----</option>
                <option value="@Constants.StatusAccount.IS_ACTIVE">Active</option>
                <option value="@Constants.StatusAccount.IS_NOT_ACTIVE">Not Active</option>
            </select>
        </td>
    </tr>    
    <tr>
        <td class="editor-label">
            @Html.LabelFor(m => m.FromDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.FromDate)
        </td>
        <td class="editor-label">
            @Html.LabelFor(m => m.ToDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.ToDate)
        </td>
    </tr>
    <tr>
         <td class="editor-label">
            @Html.LabelFor(m => m.TypePay) 
        </td>
        <td class="editor-field" colspan="3">
            <select id="TypePay" name="TypePay">
                <option>----All----</option>
                <option value="@Constants.TypePay.ONE_PAY">Onepay</option>
                <option value="@Constants.TypePay.PAY_PAL">Paypal</option>
            </select>
        </td>
    </tr>
    <tr>
        <td></td>
        <td align="left">            
            <a class="bt_red" href="#" onclick="Search(1);" style="float:left"><span class="bt_red_lft"></span><strong>Search</strong><span class="bt_red_r"></span></a>  
        </td>
        <td></td>
        <td align="right">
            <a class="bt_red" href="#" onclick="DeleteSelectedItems();" style="float:left"><span class="bt_red_lft"></span><strong>Delete Account</strong><span class="bt_red_r"></span></a>               
            <a style="float:left" href="~/Report/ExportListAccountNotIntBlackList" class="bt_red"><span class="bt_red_lft"></span><strong>Download List Account</strong><span class="bt_red_r"></span></a>
        </td>
    </tr>
    <tr>
        <td colspan="4"></td>
    </tr>
    <tr>
        <td colspan="4">
            <div id="divToTalRecord">Total record :</div>
        </td>
    </tr>
</table>
<table id="rounded-corner">
    <thead>
        <tr>        
            <th class="rounded-company" scope="col" style="width: 30px"></th>  
            <th class="rounded" scope="col" style="width: 10px"><input id="chkAll" onclick="javascript: SelectAll(this, 'ListId');" type="checkbox" /></th>
            <th class="rounded" scope="col" style="width: 30px">Id</th>          
            <th class="rounded" scope="col" style="width: 100px">Partner</th>
			<th class="rounded" scope="col" style="width: 200px">Name</th> 
            <th class="rounded" scope="col" style="width: 200px">Email</th>
            <th class="rounded" scope="col" style="width: 100px">Password</th>
            <th class="rounded" scope="col" style="width: 100px">Country</th>
            <th class="rounded" scope="col" style="width: 100px">Payment type</th>
            <th class="rounded" scope="col" style="width: 100px">Join Date</th>
            <th class="rounded" scope="col" style="width: 100px">Logon Date</th>
            <th class="rounded" scope="col" style="width: 100px">Expiry Date</th>
            <th class="rounded" scope="col" style="width: 100px">Days Remaining</th>
            <th class="rounded" scope="col" style="width: 50px">Status</th>            
            <th scope="col" style="width: 20px"></th>
            <th class="rounded-q4" scope="col" style="width: 20px"></th>
        </tr>
    </thead>  
</table>
<div id="divPager"></div>
<div id="divDialogTransactionLog" class="div_dialog" style="display: none;z-index:100000" title="List Transaction Log"> 
    <div class="clear">
    </div>
    <div style="width: 100%;"> 
        <div style="width: 100%;">                         
            <table width="100%" class="tablePopUp" id="tblDialogTransactionLog" bordercolor="#000000" border="1" cellspacing="0" cellpadding="1" style="border-collapse:collapse" class="tMedium">
                <tr>
                    <td colspan="11" align="center"><b>Email <span id="spEmail"></span></b></td>
                </tr>                        
                <tr bgcolor="#c0c0c0" class="aMedium">
                    <td align="center" width="30px"><strong>Id</strong></td>
                    <td align="center" width="100px"><strong>Partner</strong></td>
                    <td align="center" width="100px"><strong>Subscriptions</strong></td>
                    <td align="center" width="100px"><strong>Marketing</strong></td>
                    <td align="center" width="50px"><strong>Type Pay</strong></td>                            
                    <td align="center" width="50px"><strong>Coupon Code</strong></td>
                    <td align="center" width="50px"><strong>Amount</strong></td>
                    <td align="center" width="100px"><strong>Request Time</strong></td>
                    <td align="center" width="100px"><strong>Respones Time</strong></td> 
                    <td align="center" width="50px"><strong>Status</strong></td> 
                    <td align="center" width="100px"><strong>Message</strong>  
                        <input type="hidden" value=""/>                      
                    </td>                                                       
                </tr>					
            </table>
            <table width="100%">
                <tr>
                    <td align ="right"><div id="divPager1"></div></td>
                </tr>
            </table>				   
        </div>
        <div class="clear"></div>
    </div>
    <div class=" pad15 clear">
    </div> 
</div>
<script src="~/Scripts/ckeditor/ckeditor.js"></script>
<script src="~/Scripts/ckeditor/adapters/jquery.js"></script>    
<script>
    $(function () {
        $('#FromDate').datepicker();
        $('#ToDate').datepicker();
        $("#divDialogTransactionLog").dialog({
            autoOpen: false,
            modal: true,
            height: 550,
            width: 900,
            resizable: false,
            open: function (event, ui) {
                $("#divDialogTransactionLog").dialog("option", "position", "center");
            }
        });
    });
    
    function ShowDialogTransactionLog(id) {
        $("#divDialogTransactionLog").dialog("open");
        $('#spEmail').html($('#iphEmail' + id).val());
        $('#AccountId').val(id);
        GetAllTransactionLogByAccount(1);
    }
    
    function GetAllTransactionLogByAccount(PageIndex) {
        var URL = '/TransactionLog/GetAllTransactionLogByAccountId';
        $.ajax({
            url: URL,
            type: "POST",
            data: {
                accountId: $('#AccountId').val(),                
                pageIndex: PageIndex,
                pageSize: 50
            },
            success: function (data) {
                TotalPage = Math.ceil(data.TotalRecord / 50);
                if (TotalPage > 0) {
                    $("#divPager1").show();
                } else {
                    $("#divPager1").hide();
                }
                $("tr[refer='trContent1']").remove();
                var $table = $('#tblDialogTransactionLog');
                $.each(data.ListItem, function (i, item) {
                    var strHtml = "";
                    var requestTime = item.RequestTime == null ? '' : myDateFormatter(parseInt(item.RequestTime.substr(6)));
                    var responesTime = item.ResponesTime == null ? '' : myDateFormatter(parseInt(item.ResponesTime.substr(6)));
                    strHtml += "<tr refer='trContent1'>";                       
                    strHtml += "<td> " + item.Id + " </td>";
                    strHtml += "<td> " + item.PartnerCode + " </td>";					                                  
                    strHtml += "<td> " + item.Subscription + " </td>";
                    strHtml += "<td> " + item.Marketing + " </td>";   
                    if (item.TypePay == @Constants.TypePay.ONE_PAY) {
                        strHtml += "<td>Onepay</td>";
                    } else {
						if (item.TypePay == @Constants.TypePay.PAY_PAL) {
							strHtml += "<td> Paypal </td>";
						}
						else
						{
							strHtml += "<td></td>";
						}
                    }
                    strHtml += "<td> " + item.CouponCode + " </td>";
                    strHtml += "<td> " + item.Amount + " </td>";                    
                    strHtml += "<td> " + requestTime + " </td>";
                    strHtml += "<td> " + responesTime + " </td>";
                    switch (item.Status) {
                        case @Constants.TranctionLogStatus.START:
                            strHtml += "<td>Start</td>";
                            break;
                        case @Constants.TranctionLogStatus.SUCCESS:
                            strHtml += "<td>Success</td>";
                            break;
                        case @Constants.TranctionLogStatus.ERROR:
                            strHtml += "<td>Error</td>";
                            break;
                        case @Constants.TranctionLogStatus.CANCEL:
                            strHtml += "<td>Cancel</td>";  
                            break;
                        default :
                            strHtml += "<td></td>";   
                    }
                    strHtml += "<td> " + item.Message + " </td>";
                    strHtml += "</tr>";
                    $table.append(strHtml);                                    
                });
                $("#divPager1").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: GetAllTransactionLogByAccount });
            },
            complete: function (o1, o2) {
            }
        });
    }

    function Search(PageIndex) {
        var URL = '/Account/Search';
        $.ajax({
            url: URL,
            type: "POST",
            data: {
                firstName: $('#FirstName').val(),
                lastName: $('#LastName').val(),
                email: $('#Email').val(),
                countryId: $('#CountryId').val(),
                status: $('#Status').val(),
                partnerId : $('#PartnerId').val(),
                fromDate: $('#FromDate').val(),
                toDate: $('#ToDate').val(),
                typePay: $('#TypePay').val(),
                pageIndex: PageIndex,
                pageSize: 50
            },
            success: function (data) {
                $('#divToTalRecord').html('Total record :'+data.TotalRecord);
                TotalPage = Math.ceil(data.TotalRecord / 50);
                if (TotalPage > 0) {
                    $("#divPager").show();
                } else {
                    $("#divPager").hide();
                }
                $("tr[refer='trContent']").remove();
                var $table = $('#rounded-corner');
                $.each(data.ListItem, function (i, item) {
                    var strHtml = "";
                    var createDate = item.CreateDate == null ? '' : myDateFormatter(parseInt(item.CreateDate.substr(6)));
                    var startDate = item.StartDate == null ? '' : myDateFormatter(parseInt(item.StartDate.substr(6)));
                    var endDate = item.EndDate == null ? '' : myDateFormatter(parseInt(item.EndDate.substr(6)));
                    var expiresDate = Math.floor((new Date(endDate) -new Date()) / (1000 * 60 * 60 * 24));
                    strHtml += "<tr refer='trContent'>";  
                    strHtml += "<td> " + ((PageIndex-1)*50 + i+1) + " <input type='hidden' id='iphEmail"+ item.Id +"' value="+ item.Email +" /></td>";
                    strHtml += "<td><input type='checkbox' value='true' refer='chk' onclick=javascript:SelectOne('ListId') name='check' itemid='" + item.Id + "' id='check'></td>";
                    strHtml += "<td> " + item.Id + " </td>";
                    strHtml += "<td> " + item.PartnerCode + " </td>";
                    strHtml += "<td> " + item.FirstName + " " + item.LastName + "</td>";
                    strHtml += "<td> <a href='javascript:ShowDialogTransactionLog("+ item.Id  +")'>" + item.Email + "</a> </td>";
                    strHtml += "<td> " + item.PassWord + " </td>";
                    strHtml += "<td> " + item.Country + " </td>";
                    if (item.TypePay == @Constants.TypePay.ONE_PAY) {
                        strHtml += "<td> One Pay </td>";
                    } else {
                        if (item.TypePay == @Constants.TypePay.PAY_PAL) {
							strHtml += "<td> Pay pal </td>";
						}
						else
						{
							strHtml += "<td></td>";
						}
                    }
                    strHtml += "<td> " + createDate + " </td>";
                    strHtml += "<td> " + startDate + " </td>";
                    strHtml += "<td> " + endDate + " </td>";
                    if(!isNaN(expiresDate)) {
                        strHtml += "<td> " + expiresDate + " </td>";
                    } else {
                        strHtml += "<td>  </td>";
                    }
                    if (item.Status == @Constants.StatusAccount.IS_ACTIVE) {
                        strHtml += "<td> Acitve </td>";
                    } else {
                        strHtml += "<td> Not Acitve </td>";
                    }
                    strHtml += "<td><a href='/Account/Edit?accountId="+item.Id+"'><img src='/Content/Images/user_edit.png' /></a></td>";                   
                    strHtml += "<td><a href='/Account/ResetPassword?accountId="+item.Id+"'><img src='/Content/Images/reset_pass.gif' width='16px' /></a></td>";                   
                    strHtml += "</tr>";
                    $table.append(strHtml);
                });
                $("#divPager").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: Search });
            },
            complete: function (o1, o2) {
            }
        });
    }
    
    function DeleteSelectedItems() {        
        if (confirm('Bạn có muốn xóa dữ liệu?')) {
            var hidSelected = $("input[id='ListId']");
            if (hidSelected.val() != "") {                
                $.ajax({
                    url: urlpath + 'Account/Delete',
                    type: 'POST',
                    data: {
                        listId: hidSelected.val()
                    },
                    success: function (data) {                        
                        alert('Bạn đã xóa dữ liệu thành công!');                        
                        Search(1);
                    },
                    complete: function (o1, o2) {
                    }
                });
            }
        }
    };
</script>
